package studio.raptor.databus.dashboard.controller;

import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import studio.raptor.databus.dashboard.properties.DispatcherConfig;
import studio.raptor.databus.dashboard.properties.TaskConfig;
import studio.raptor.databus.dashboard.service.interfaces.TaskService;

/**
 * @author jack
 * @since 1.0
 */
@RestController
@RequestMapping("/dispatcher")
public class TaskController {

  private static final Logger log = LoggerFactory.getLogger(TaskController.class);

  @Autowired
  TaskService taskService;

  @PostMapping(value = "/create")
  public String createDispatcher(@RequestBody @Valid DispatcherConfig dispatcherConfig) {
    return taskService.createDispatcher(dispatcherConfig.getIp(), dispatcherConfig.getPort(),
        dispatcherConfig.getName());
  }

  @PostMapping(value = "/remove")
  public String removeDispatcher(@RequestBody @Valid DispatcherConfig dispatcherConfig) {
    return taskService.removeDispatcher(dispatcherConfig.getIp(), dispatcherConfig.getPort());
  }

  @PostMapping(value = "/task/create")
  public TaskConfig createTask(@RequestBody @Valid TaskConfig taskConfig) {
    return taskService.createDispatcherTask(taskConfig);
  }

  @PostMapping(value = "/task/status/update")
  public TaskConfig updateTask(@RequestBody @Valid TaskConfig taskConfig) {
    return taskService.updateDispatcherTask(taskConfig);
  }


}
